﻿jQuery(document).ready(function () {

});

function BranchSearch() {
    var branchNo = jQuery('#mbranchno').val();
    getBranchDetails(branchNo, function (result) {
        jQuery('#branchform').form('clear');
        jQuery('#branchform').form('load', result.BranchDetails);
        jQuery('#branchform').form('load', result.CustomerDetails);
        jQuery('#MatAll').datagrid({
            data: result.MaterialInBranch
        });
        jQuery('#matnumber').text(result.MaterialInBranch.length);
    });
}

function getBranchDetails(branchNo, func) {
    if (branchNo != '') {
        jQuery.ajax({
            type: "POST",
            url: "/api/BranchApi/PostSearchMaterialByIds?branchno=" + branchNo,
            success: function (result) {
                if (func) {
                    func(result);
                }
            },
            error: function (result) {
                if (func) {
                    func(result);
                }
            }
        });
    }
}

function exportMatInBranch(branhNo, func) {
    var branchNo = jQuery('#mbranchno').val();
    if (branchNo != '') {
        window.open("/api/BranchApi/GetExportMatByBranch?branchno=" + branchNo, "_blank", "toolbar=no, scrollbars=no, resizable=yes, top=500, left=500, width=400, height=400");
    }
    else {
        alert("กรุณาระบุ branch id ในช่อง  Branch No.");
    }
}

function Save(isdraft) {
    var mbranchnoValue = jQuery.trim(jQuery('#mbranchno').val());
    var mprovdescValue = jQuery.trim(jQuery('#mprovdesc').val());
    var mcustnoValue = jQuery.trim(jQuery('#mcustno').val());
    var mprovtransValue = jQuery.trim(jQuery('#mprovtrans').val());
    var mproviceValue = jQuery.trim(jQuery('#mprovice').val());
    var mprovtran_grpValue = jQuery.trim(jQuery('#mprovtran_grp').val());

    var branchsave = {
        mbranchno: jQuery.trim(mbranchnoValue),
        mprovdesc: jQuery.trim(mprovdescValue),
        mcustno: jQuery.trim(mcustnoValue),
        mprovtrans: jQuery.trim(mprovtransValue),
            mprovice: jQuery.trim(mproviceValue),
        mprovtran_grp: jQuery.trim(mprovtran_grpValue)
    };

    jQuery.ajax({
        type: "POST",
        url: "/api/BranchApi/PostSaveBranch",
        data: JSON.stringify(branchsave),
        contentType: "application/json",
        success: function (result) {
            if (result == true) {
                alert('Save Success');
            }
        }

    });
}

function clearForm() {
    jQuery('#branchform').form('clear');
}

function clearfilter() {
    BranchSearch();
}


function doSearch(value) {
    var allMats = jQuery('#MatAll').datagrid('getRows');
    var filterMats = jQuery.grep(allMats, function (a) {
        return a.mmatno.indexOf(value) > -1;
    });

    jQuery('#MatAll').datagrid({
        data: filterMats
    });
}

function doSearchBranch(value) {
    var allMats = jQuery('#MatAll').datagrid('getRows');
    var filterMats = jQuery.grep(allMats, function (a) {
        return a.meancode.indexOf(value) > -1;
    });

    jQuery('#MatAll').datagrid({
        data: filterMats
    });
}

function CustomerSearchPopup() {
    jQuery('#customerSearch').dialog('open').dialog('setTitle', 'ค้นหาลูกค้า');
}


function BranchSearchPopup() {
    jQuery('#branchSearch').dialog('open').dialog('setTitle', 'ค้นหา Branch');
}

function searchCustomer() {
    var cutomerThaiName = jQuery.trim(jQuery('#searchCustomerNameThai').val());

    if (cutomerThaiName != '' ) {

        var Query = "name=" + cutomerThaiName;
        jQuery('#dlgCustomerSearch').datagrid({
            url: "/api/BranchApi/PostCustomerByName?" + Query,
            //loadFilter: pagerFilter,
            //onDblClickRow: function (index, fields, value) {
            //    editNonHR(fields);
            //}
        });
    }
    else {
        alert('กรุณาเลือกเงื่อนไขในการค้นหาลูกค้า');
    }
}


function searchBranch() {
    var BranchNo = jQuery.trim(jQuery('#branchNo').val());
    var BranchName = jQuery.trim(jQuery('#branchName').val());


        var Query = "branchno=" + BranchNo + "&branchname=" + BranchName;
        jQuery('#dlgBranchSearch').datagrid({
            url: "/api/BranchApi/PostBranchByNameAndId?" + Query,
            //loadFilter: pagerFilter,
            //onDblClickRow: function (index, fields, value) {
            //    editNonHR(fields);
            //}
        });
    
}

function filterByMatNo() {
    var matno = jQuery('#Matnofilter').val();
    var branchno = jQuery('#Barcodefilter').val();
    var allMats = jQuery('#MatAll').datagrid('getRows');

    var filterMats = allMats;
    if (matno != undefined && matno != '') {
        filterMats = jQuery.grep(allMats, function (a) {
            return a.mmatno.indexOf(matno) > -1;
        });
    }

    if (branchno != undefined && branchno != '') {
        filterMats = jQuery.grep(allMats, function (a) {
            return a.meancode.indexOf(branchno) > -1;
        });
    }

    jQuery('#MatAll').datagrid({
        data: filterMats
    });
}

function selectCustomer() {

    var customerSelect = jQuery("#dlgCustomerSearch").datagrid("getSelected");

    //jQuery("#mcustno").textbox('setValue', customerSelect.mcustno);
    //jQuery("#mcustname1").textbox('setValue', customerSelect.mcustname1);
    jQuery('#branchform').form('load', customerSelect);
    jQuery('#customerSearch').dialog('close')
}

function selectBranch() {

    var branchSelect = jQuery("#dlgBranchSearch").datagrid("getSelected");
    if (branchSelect == null || branchSelect == undefined) {
        alert("กรุณาเลือก Brach");
    }
    else {

        jQuery("#mbranchno").textbox('setValue', branchSelect.mbranchno);
        jQuery('#branchSearch').dialog('close')
        BranchSearch();
    }
}